﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace _5E.Infrastructure.Querying
{
    public class Query
    {
        private IList<Query> m_SubQueries = new List<Query>();
        public IEnumerable<Query> SubQueries { get { return m_SubQueries; } }

        private IList<Criterion> m_Criterions = new List<Criterion>();
        public IEnumerable<Criterion> Criterions { get { return m_Criterions; } }

        private QueryOperator m_QueryOperator = QueryOperator.And;
        public QueryOperator QueryOperator
        {
            get { return m_QueryOperator; }
            set { m_QueryOperator = value; }
        }

        private IList<OrderByClause> m_Orders = new List<OrderByClause>();
        public IEnumerable<OrderByClause> Orders { get { return m_Orders; } }

        public void AddSubQuery(Query subQuery)
        {
            m_SubQueries.Add(subQuery);
        }

        public void Add(Criterion criterion)
        {
            m_Criterions.Add(criterion);
        }

        public void Add<T>(Expression<Func<T, object>> expression, CriteriaOperator criteriaOperator, object value = null)
        {
            Criterion criterion = Criterion.Create<T>(expression, criteriaOperator, value);
            Add(criterion);
        }

        public void AddOrder<T>(Expression<Func<T, object>> expression, bool asc = true)
        {
            OrderByClause order = OrderByClause.Create<T>(expression, asc);
            m_Orders.Add(order);
        }
    }
}
